package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.OrderRefundApplicate;
import com.addplus.townmall.server.api.model.town.expand.OrderRefundApplicateExpand;
import com.addplus.townmall.server.api.modelenum.OrderRefundEnum;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商城订单退款申请表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface OrderRefundApplicateMapper extends BaseAddMapper<OrderRefundApplicate> {

    @Select("SELECT\n" +
            " a.id,\n" +
            " a.order_id,\n" +
            " a.refund_num,\n" +
            " a.order_refund_enum,\n" +
            " a.store_id,\n" +
            " a.store_name,\n" +
            " a.store_icon_path,\n" +
            " a.member_id,\n" +
            " a.refund_quantity,\n" +
            " a.refund_total_account,\n" +
            " a.refund_type\n" +
            "FROM\n" +
            " order_refund_applicate a \n" +
            "WHERE\n" +
            " a.member_id = #{memberId} \n" +
            " AND a.is_delete = 0 \n" +
            "ORDER BY\n" +
            " a.gmt_create DESC ")
    List<OrderRefundApplicateExpand> getOrderRefundApplicateExpandList(Page page, @Param("memberId") Integer memberId);

    @Select("SELECT\n" +
            " a.id,\n" +
            " a.store_id,\n" +
            " a.store_name,\n" +
            " a.store_icon_path,\n" +
            " a.refund_type,\n" +
            " a.refund_num,\n" +
            " a.order_refund_enum,\n" +
            " a.refund_total_account,\n" +
            " a.refund_freight,\n" +
            " a.refund_shop_account,\n" +
            " a.refund_quantity,\n" +
            " a.goods_status,\n" +
            " a.reason,\n" +
            " a.reason_detail,\n" +
            " a.apply_date,\n" +
            " a.shop_courier_id,\n" +
            " a.shop_courier_name,\n" +
            " a.shop_courier_number,\n" +
            " a.shop_courier_certpic_img,\n" +
            " a.contact_cell_phone,\n" +
            " a.remark,\n" +
            " s.sender_phone,\n" +
            " s.return_name,\n" +
            " s.return_phone,\n" +
            " s.return_address\n" +
            "FROM\n" +
            " order_refund_applicate a\n" +
            " LEFT JOIN store s ON s.id = a.store_id AND s.is_delete = 0 \n" +
            "WHERE\n" +
            " a.id = #{refundId} \n" +
            " AND a.member_id = #{memberId} \n" +
            " AND a.is_delete = 0")
    OrderRefundApplicateExpand getOrderRefundGress(@Param("memberId") Integer memberId, @Param("refundId") Integer refundId);

    @Select("select ora.* from town_order ord " +
            "LEFT JOIN order_refund_applicate ora on ora.order_id = ord.id and ora.is_delete = 0 " +
            "LEFT JOIN order_refund_applicate_items orai on orai.refund_id = ora.id and orai.is_delete = 0 and orai.order_item_id = ${id} and orai.shop_product_type = ${orderProductType} " +
            "where ord.order_number = ${orderNumber} and ord.is_delete = 0")
    OrderRefundApplicate getOrderRefundApplicate(@Param("id")Integer id, @Param("orderNumber")String orderNumber, @Param("orderProductType")Integer orderProductType);

    @Select("SELECT\n" +
            " w.id,\n" +
            " w.member_id,\n" +
            " w.store_id,\n" +
            " w.store_name,\n" +
            " w.store_icon_path,\n" +
            " w.refund_num,\n" +
            " w.refund_quantity,\n" +
            " w.refund_total_account,\n" +
            " w.refund_commis_amount,\n" +
            " w.order_refund_enum,\n" +
            " w.gmt_create \n" +
            "FROM\n" +
            " order_refund_applicate w\n" +
            " LEFT JOIN member m ON m.id = w.member_id \n" +
            " AND w.is_delete = 0 \n" +
            "WHERE\n" +
            " m.invite_member_id = #{memberId} \n" +
            " AND m.is_deleted = 0 \n" +
            " AND w.order_refund_enum != 'CLOSE' \n" +
            "ORDER BY\n" +
            " w.gmt_create DESC")
    List<OrderRefundApplicateExpand> getOrderDistributionOrderRefund(Page page, @Param("memberId") Integer memberId);

    @Select("<script> \n" +
            "SELECT\n" +
            " w.*,\n" +
            " m.account,\n" +
            " t.order_number,\n" +
            " t.shop_total_amount\n" +
            "FROM\n" +
            " order_refund_applicate w\n" +
            " LEFT JOIN member m ON m.id = w.member_id \n" +
            " LEFT JOIN town_order t ON t.id = w.order_id \n" +
            "WHERE\n" +
            " w.is_delete = 0 And w.store_id=#{store_id}\n" +
            "<if test='apply_date!=null '>" +
            "AND DATE_FORMAT(w.apply_date,'%Y-%m-%d')=DATE_FORMAT(#{apply_date},'%Y-%m-%d')" +
            "</if>" +
            "<if test='gmt_modified!=null '>" +
            "AND DATE_FORMAT(w.gmt_modified,'%Y-%m-%d')=DATE_FORMAT(#{gmt_modified},'%Y-%m-%d')" +
            "</if>" +
            "<if test='order_number!=null'>" +
            "AND t.order_number like concat('%',#{order_number},'%')" +
            "</if>" +
            "<if test='strList!=null'>" +
            "AND w.order_refund_enum IN " +
            "<foreach item='item' index='index' collection='strList' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "ORDER BY\n" +
            " w.gmt_modified DESC" +
            "</script>")
    List<OrderRefundApplicateExpand> getAllOrderRefund(IPage page, @Param("apply_date") Date apply_date, @Param("gmt_modified") Date gmt_modified, @Param("order_number") String order_number, @Param("store_id") Integer store_id, @Param("strList") List<String> strList);

    @Select("SELECT\n" +
            " w.*,\n" +
            " m.*,\n" +
            " t.order_number,\n" +
            " t.shop_total_amount\n" +
            "FROM\n" +
            " order_refund_applicate w\n" +
            " LEFT JOIN member m ON m.id = w.member_id \n" +
            " LEFT JOIN town_order t ON t.id = w.order_id \n" +
            "WHERE\n" +
            " w.is_delete = 0 And w.id=#{id}")
    OrderRefundApplicateExpand getOrderRefundById(@Param("id") Integer id);
}
